<template>
	<view class="order-wrapper" :style="{paddingTop: paddingTop + 'px'}">
		
		<navBar
			title="退款/售后"
			:customBack="true"
			@back="handleBack"
			@home="handleHome"
		/>
		
		<view class="order-tab" :style="{top: paddingTop + 'px'}">
			<u-tabs
				:list="list"
				:scrollable="false"
				:current="current"
				lineColor="#CF1A1B"
				lineWidth="70"
				lineHeight="4"
				:activeStyle="{
	  			color: '#CF1A1B',
				'font-size':'14px'
	  		}"
				:inactiveStyle="{
	  			color: 'rgba(0,0,0,0.65)',
				'font-size':'14px'
	  		}"
				itemStyle="height: 44px;"
				@change="handleTab"
			>
			</u-tabs>
		</view>
		<view v-if="orderList.length == 0" class="empty">
			<image src="@/static/empty.png"></image>
			<text>无数据</text>
		</view>
		<!-- 退款列表 -->
		<view class="list-wrapper" v-if="current === 0">
			<view class="order-list" v-for="item in orderList">
				<view class="order-item">
					<view class="order-item-h">
						<view class="store-name">
							<text class="label">退款单号：</text>
							<text class="name">{{ item.refundOrderNo }}</text>
						</view>
						<view class="order-status">
							<text >退款</text>
						</view>
					</view>
					<view class="order-item-c">
						<view class="goods-list">
							<view class="goods-item" v-for="(list,index) in item.orderItems">
								<view class="goods-img">
									<image class="img"
										:src="list.image ? list.image : 'https://coach-vehicle-uat-1313990257.cos.ap-beijing.myqcloud.com/miniapp/order/goods_default_square.png'">
									</image>
								</view>
								<view class="goods-info"
									:class="{'end-goods-info':index == (item.orderItems.length - 1) }">
									<view class="info">
										<view class="goods-name">{{ list.goodsName }}</view>
										<!-- <view class="goods-pid">{{ list.propertiesDesc }}</view> -->
										<view class="goods-price">
											<view class="price">
												<text class="label">单价：</text>
												<text>￥</text>{{ list.sellPrice.toFixed(2) }}
											</view>
											<view class="num">
												<text class="label">申请数量：</text>
												<text class="value">{{ list.quantity }}</text>
											</view>
										</view>
									</view>
								</view>
							</view>
						</view>
						
						<!-- 状态 -->
						<view class="goods-state">
							<view class="gs-item">
								<view class="gsi-label">
									<view class="gsil-tag"></view>
									<view class="gsil-state">
										{{ getOrderState(item.orderState) }}
									</view>
								</view>
								<view class="gsi-value">
									<view v-if="item.orderState === 1" class="">
										已提交申请，待门店审核
									</view>
									<view v-if="item.orderState === 3" class="">
										门店已审核，待平台审核
									</view>
									<view v-if="item.orderState === 4 || item.orderState === 7" class="">
										退款已关闭，{{ item.rejectReason }}
									</view>
									<view v-if="item.orderState === 5" class="">
										退款成功￥{{ item.refundMoney.toFixed(2) }}
									</view>
                  <view v-if="item.orderState === 6" class="">
										退款中，预计退款￥{{ item.refundMoney.toFixed(2) }}
									</view>
									
								</view>
							</view>
						</view>
					</view>
					
					<view class="order-item-b">
						<!-- 退款订单状态, 1:待审核；3:平台待审核；4：退款关闭；5:退款成功；6:退款中；7:平台驳回 -->
						<!--  1:待审核；3:待审核；4：退款关闭；5:退款成功；6:退款中；7:退款关闭 -->
						<!-- 退款中和退款成功 - 显示进度 -->
						<button v-if="item.orderState === 6 || item.orderState === 5" type="warn" size="mini" :plain="true" @click.stop="refundProgress(item)">退款进度</button>
						<button type="warn" size="mini" :plain="true" @click.stop="refundDetail(item)">查看详情</button>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 售后列表 -->
		<view class="list-wrapper" v-if="current === 1">
			<view class="order-list" v-for="item in orderList">
				<view class="order-item">
					<view class="order-item-h">
						<view class="store-name">
							<text class="label">售后单号：</text>
							<text class="name">{{ item.serviceNo }}</text>
						</view>
						<view class="order-status">
							<text >售后</text>
						</view>
					</view>
					<view class="order-item-c">
						<view class="goods-list">
							<view class="goods-item" >
								<view class="goods-img">
									<image class="img"
										:src="item.orderItem.image ? item.orderItem.image : 'https://coach-vehicle-uat-1313990257.cos.ap-beijing.myqcloud.com/miniapp/order/goods_default_square.png'">
									</image>
								</view>
								<view class="goods-info" style="border-bottom: none;">
									<view class="info">
										<view class="goods-name">{{ item.orderItem.goodsName }}</view>
										<view class="goods-price" style="margin-top: 30rpx;">
											<view class="price">
												<text class="label">申请数量：</text>
												<text>{{ item.quantity }}</text>
											</view>
										<!-- 	<view class="num">
												<text class="label">{{ item.issueTopic }} > {{ item.issueType }}</text>
											</view> -->
										</view>
										<view class="goods-price" style="margin-top: 30rpx;">
											<view class="price">
												<text class="label">{{ item.issueTopic }} > {{ item.issueType }}</text>
											</view>
										</view>
									</view>
								</view>
							</view>
						</view>
						
						<!-- 状态 -->
						<view class="goods-state">
							<view class="gs-item">
								<view class="gsi-label">
									<view class="gsil-tag"></view>
									<view class="gsil-state">
										{{ getOrderState1(item.state) }}
									</view>
								</view>
								<!-- 售后单状态 1待受理、2已受理 3已派单 4已接单 5待鉴定、6处理中、7已完成、8已关闭 -->
								<view class="gsi-value">
									<view v-if="item.state === 1" class="">
										您的申请已提交，待受理
									</view>
									<view v-if="item.state === 2" class="">
										客服已受理
									</view>
									<view v-if="item.state === 3" class="">
										客服已派单，待商家接单
									</view>
									<view v-if="item.state === 4" class="">
										商家已接单，处理中
									</view>
									<view v-if="item.state === 7" class="">
										处理完成
									</view>
									<view v-if="item.state === 8" class="">
										售后已关闭，{{ item.serviceStaffNote }}。
									</view>
									
								</view>
							</view>
						</view>
					</view>
					
					<view class="order-item-b">
						<button type="warn" size="mini" :plain="true" @click.stop="afterSaleDetail(item)">查看详情</button>
					</view>
				</view>
			</view>
		</view>
		
		
		
		<uni-load-more :status="status" v-if="loadVisible" :contentText="contentText" />
	</view>
</template>

<script>
	import navBar from '@/components/navBar.vue'
	import {
		mapGetters
	} from 'vuex'
	export default {
		components: {
			navBar
		},
		data() {
			return {
				paddingTop: '',
				current: 0,
				list: [{
						name: '退款',
						status: 6
					}, {
						name: '售后',
						status: 1,
					}
				],
				pageNum: 1,
				pageSize: 10,
				orderList: [],
				total: 0,
				emptyVisible: false,
				status: 'more',
				loadVisible: false,
				contentText: {
					contentdown: "上拉显示更多",
					contentrefresh: "正在加载...",
					contentnomore: "没有更多数据了",
				},
			}
		},
		computed: {
			...mapGetters('user', ['getUserInfo']),
			// 订单状态
			getOrderState(){
				return (state) => {
					// 1:待审核；3:平台待审核；4：退款关闭；5:退款成功；6:退款中；7:平台驳回
					// 1:待审核；3:待审核；4：退款关闭；5:退款成功；6:退款中；7:退款关闭
					switch(state){
						case 1:
							return '待审核'
						case 3:
							return '待审核'
						case 4:
							return '退款关闭'
						case 5:
							return '退款成功'
						case 6:
							return '退款中'
						case 7:
							return '退款关闭'
						default:
							return ''
					}
				}
			},
			getOrderState1(){
				// 售后单状态 1待受理、2已受理 3已派单 4已接单 5待鉴定、6处理中、7已完成、8已关闭
				return (state) => {
					switch(state){
						case 1:
							return '待受理'
						case 2:
							return '已受理'
						case 3:
							return '已派单'
						case 4:
							return '已接单'
						case 7:
							return '已完成'
						case 8:
							return '已关闭'
						default:
							return ''
					}
				}
			}
		},
		methods: {
			handleBack(){
				const source = uni.getStorageSync('source')
				if(source){
					//  refund、afterSale
					if (source === 'refund'){
						uni.redirectTo({
						  url: `/orderPack/list/index?status=4`,
						})
					} else {
						uni.redirectTo({
						  url: `/orderPack/list/index?status=6`,
						})
					}
					uni.removeStorageSync('source')
				}else {
					uni.navigateBack()
				}
			},
			handleHome(){
				uni.switchTab({
					url: '/pages/index/index/index'
				})
			},
			handleTab(e) {
				this.pageNum = 1
				this.orderList = []
				this.current = e.index
				this.initData()
			},
			//获取数据
			async initData() {
				uni.showLoading({
					title: '加载中...'
				})
				try {
					const data = {
						pageNum: this.pageNum,
						pageSize: this.pageSize,
					}
					if(this.current === 0){
						const res = await this.$request({
							method: 'GET',
							url: '/order-center/carOwner/refund/orders',
							data
						})
						this.orderList = this.orderList.concat(res.datas.data)
						this.total = res.datas.count
					}else {
						const res = await this.$request({
							method: 'POST',
							url: '/order-center/order-customer-service/userPage',
							data
						})
						this.orderList = this.orderList.concat(res.data)
						this.total = res.count
					}


					this.loadVisible = this.total > this.pageSize
					this.emptyVisible = this.total === 0
					uni.hideLoading()
				} catch (res) {
					uni.hideLoading()
					uni.showToast({
						title: '请求失败',
						icon: 'error'
					})
				}

				uni.stopPullDownRefresh();
			},
			refundProgress(item) {
				uni.navigateTo({
					url: `/orderPack/aftermarket/refundProgress?id=${item.id}`,
				})
			},
			refundDetail(item){
				uni.navigateTo({
					url: `/orderPack/aftermarket/refundDetail?id=${item.id}`,
				})
			},
			afterSaleDetail(item){
				uni.navigateTo({
					url: `/orderPack/aftermarket/afterSaleDetail?id=${item.id}`,
				})
			}
		},

		onReachBottom() {
			if (this.orderList.length >= this.total) {
				this.status = 'noMore'
				return
			}
			this.loadVisible = true
			this.status = 'loading'
			this.pageNum++
			this.initData()
		},
		onPullDownRefresh() {
			this.loadVisible = false
			this.pageNum = 1
			this.total = 0
			this.orderList = []
			this.initData()
		},
		onLoad(query) {
			this.current = query.current - 0 || 0
			this.initData()
			this.paddingTop = this.$store.getters['global/getSystemInfo'].navBarHeight
		}

	}
</script>

<style lang="scss" scoped>
	.order-wrapper {
		width: 100%;
		height: 100%;
		font-size: 14px;
		box-sizing: border-box;
		padding-bottom: env(safe-area-inset-bottom);

		/deep/ .u-tabs__wrapper__nav__line {
			margin-left: 16px;
		}

		/deep/ .u-tabs__wrapper__nav__item {
			padding: 0;
		}

		.empty {
			width: 100%;
			height: 80%;
			position: absolute;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;

			image {
				width: 220px;
				height: 180px;
			}

			text {
				font-size: 30rpx;
				color: rgba(0, 0, 0, 0.45);
				line-height: 30rpx;
			}
		}

		.order-tab {
			z-index: 99;
			position: fixed;
			width: 100%;
			background-color: #FFFFFF;
		}

		.list-wrapper {
			padding-top: 108rpx;
		}

		.order-list {
			.order-item {
				background: #fff;
				margin-bottom: 20rpx;
				box-sizing: border-box;
				padding: 22rpx 32rpx;
				

				.order-item-h {
					display: flex;
					justify-content: space-between;
					align-items: center;
					align-content: center;
					border-bottom: 2rpx solid rgba(0, 0, 0, 0.1);
					padding-bottom: 30rpx;			
					font-size: 14px;

					.store-name {
						display: flex;
						flex: 1;
						color: rgba(0, 0, 0, 0.85);
						width: calc(100% - 200rpx);

						.name {
							flex: 1;
							max-width: calc(100% - 46rpx);
							word-wrap: break-all;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-line-clamp: 1;
							text-shadow: 0 0 0.1px currentcolor;
							line-clamp: 1;
							-webkit-box-orient: vertical;
						}
					}

					.order-status {
						width: 200rpx;
						text-align: right;
						color: rgba(0, 0, 0, 0.65);
					}
				}

				.order-item-c {

					.goods-list {
						border-bottom: 2rpx solid rgba(0, 0, 0, 0.1);
					}

					.goods-item {
						display: flex;
						justify-content: flex-start;
						align-items: center;
						padding: 28rpx 0;

						.goods-img {
							height: 176rpx;
							width: 176rpx;
							margin-right: 26rpx;

							.img {
								width: 100%;
								height: 100%;
							}
						}

						.goods-info {
							flex: 1;
							display: flex;
							height: 180rpx;
							justify-content: flex-start;
							align-items: center;
							width: calc(100% - 200rpx);
							border-bottom: 2rpx solid rgba(0, 0, 0, 0.1);

							.info {
								width: 100%;
							}

							.goods-name {
								font-size: 15px;
								font-weight: 500;
								color: rgba(0, 0, 0, 0.85);
								margin-bottom: 14rpx;
								word-wrap: break-all;
								overflow: hidden;
								text-overflow: ellipsis;
								display: -webkit-box;
								-webkit-line-clamp: 2;
								line-clamp: 2;
								-webkit-box-orient: vertical;
								text-shadow: 0 0 0.1px currentcolor;
							}

							.goods-pid {
								margin-bottom: 22rpx;
								color: rgba(0, 0, 0, 0.65);
							}

							.goods-price {
								display: flex;
								width: 100%;
								// justify-content: space-between;
								align-items: center;
								margin-top: 48rpx;

								.price, .num {
									font-size: 13px;
									color: #EE3B3B;
									.label {
										color: rgba(0, 0, 0, 0.65);
									}
								}

								.num {
									margin-left: 40rpx;
								}
							}
						}

						.end-goods-info {
							border-bottom: none;
						}
					}
				}
				
				//	状态
				.goods-state {
					box-sizing: border-box;
					margin: 40rpx 0;
					padding: 32rpx 0 36rpx;
					background-color: rgba(255, 242, 242, 1);
					border-radius: 20rpx;
					border: 2rpx solid rgba(255, 222, 222, 1);
					.gs-item {
						display: flex;
						align-items: center;
						font-size: 14px;
						.gsi-label {
							display: flex;
							align-items: center;
							margin-right: 64rpx;
							color: rgba(0, 0, 0, 0.85);
							text-shadow: 0 0 0.1px currentcolor;
							.gsil-tag {
								width: 10rpx;
								height: 10rpx;
								background-color: rgba(207, 26, 27, 1);
								border-radius: 50%;
								margin: 0 12rpx 0 32rpx;
							}
						}
						.gsi-value {
							color: rgba(0, 0, 0, 0.65);
							flex: 1;
							word-break: break-all;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-line-clamp: 1;
							line-clamp: 1;
							-webkit-box-orient: vertical;
						}
					}
				}

				.order-item-b {
					display: flex;
					justify-content: flex-end;
					height: auto;
					overflow: hidden;
					
					button {
						max-height: 32px;
						margin: 0 0 0 24rpx;
						border-color: rgba(207, 26, 27, 1);
						color: rgba(207, 26, 27, 1);
					}
				}
			}
		}
	}
</style>

